package com.example.mylibrary.callback;

import com.example.mylibrary.exceptiopn.ExceptionEngine;
import com.google.gson.JsonElement;

import io.reactivex.annotations.NonNull;

public abstract class BaseCallBack<T> extends BaseObserver {
    boolean isConvert = true;

    @Override
    public void onNext(@NonNull Object o) {
        T bean = parse(o.toString());
        if (isConvert && isSucessFull()) {
            onSucess(bean);
        }
    }

    //解析数据
    T parse(String json) {
        T t = null;
        try {
            t = onConver(json);
            isConvert = true;
        } catch (Exception e) {
            isConvert = false;
            error("解析异常", ExceptionEngine.ANALYTIC_SERVER_DATA_ERROR);
        }
        return t;
    }

    //子类扩展解析
    protected abstract T onConver(String result);

    //回调我们的数据
    protected abstract void onSucess(T t);

    //解析我们具体的类
    public abstract T convert(JsonElement jsonElement);

    public abstract boolean isSucessFull();


}
